<?php

Class DAO {
	
	function save($vo) {
		if ($vo->getId() == null) {
			$this->insert($vo);
		} else {
			$this->update($vo);
		}
	}
	
	function count($criteria){
		return count($this->find($criteria));
	}
	
	function getFromResult($result) {
		$values = array();
		while($value = mysql_fetch_assoc($result)) {
			$values[] = $value; //se podría pasar un array de objetos VO?
		}
		return $values;
	}
	
	function checkError($result){
		require('conf/configuration.php');
		if (!$result){
        	if ($ambiente=='desarrollo'){
				throw new Exception(mysql_error());
        	}
        	else{
        		return "error";
        	}
	    }
	}
	
	function getConditionsAbs($criteria,$tabla){
		$conditions = "";

		//valor predeterminado es 0, no borrado
		if ($criteria->getBorrado() == '1' ){
			$conditions.= " AND $tabla.borrado = '1'";
		}
		else {
			$conditions.= " AND $tabla.borrado = '0'";
		}
		if ($criteria->getOrderby() != null){
			$conditions.= " ORDER BY ".$criteria->getOrderby();
		}
	    if ($criteria->getOrderby() != null and $criteria->getOrder() != null ){
			$conditions.= " ".$criteria->getOrder();
		}
		if ($criteria->getFin() != null){
			$conditions.= " LIMIT ".$criteria->getInicio().",".$criteria->getFin();
		}
		return $conditions; 
	}
	
	
}

?>